//
// Created by hay boy on 2022/7/18.
//
#include "binaryTreeNode.h"
#include "../../queue/ArrayQueue.h"
#include <iostream>
using namespace std;

template<class T>
void binaryTreeNode<T>::preOrder(binaryTreeNode<T> *t) {
    if(t!= nullptr){
        visit(t);
        preOrder(t->leftChild);
        preOrder(t->rightChild);
    }
}
template<class T>
void binaryTreeNode<T>::inOrder(binaryTreeNode<T> *t) {
    if(t!= nullptr){
        inOrder(t->leftChild);
        visit(t);
        inOrder(t->rightChild);
    }
}

template<class T>
void binaryTreeNode<T>::postOrder(binaryTreeNode<T> *t) {
    if(t!= nullptr){
        postOrder(t->leftChild);
        postOrder(t->rightChild);
        postOrder(t);
    }
}

template<class T>
void binaryTreeNode<T>::visit(binaryTreeNode<T> *x) {
    cout<<x->element<<' ';
}

template<class T>
void binaryTreeNode<T>::infix(binaryTreeNode<T> *t) {
    if(t!= nullptr){
        cout<<'(';
        infix(t->leftChild);
        cout<<t->element;
        infix(t->rightChild);
        cout<<')';
    }
}

template<class T>
void binaryTreeNode<T>::levelOrder(binaryTreeNode<T> *t) {
    ArrayQueue<binaryTreeNode<T>*> q;
    while(t!= nullptr){
        visit(t);
        if(t->leftChild!= nullptr){
            q.push(t->leftChild);
        }
        if(t->rightChild!= nullptr){
            q.push(t->rightChild);
        }
        try{
            t = q.front();
        }catch (QueueEmpty&){
            return;
        }
        q.pop();
    }

}
